package cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.service.logistics;

import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.controller.admin.logistics.vo.LogisticsCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.controller.admin.logistics.vo.LogisticsExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.controller.admin.logistics.vo.LogisticsPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.controller.admin.logistics.vo.LogisticsUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.convert.logistics.LogisticsConvert;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.dal.dataobject.logistics.LogisticsDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.dal.mysql.logistics.LogisticsMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 小程序-物流公司 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LogisticsServiceImpl implements LogisticsService {

    @Resource
    private LogisticsMapper logisticsMapper;

    @Override
    public Integer createLogistics(LogisticsCreateReqVO createReqVO) {
        // 插入
        LogisticsDO logistics = LogisticsConvert.INSTANCE.convert(createReqVO);
        logisticsMapper.insert(logistics);
        // 返回
        return logistics.getId();
    }

    @Override
    public void updateLogistics(LogisticsUpdateReqVO updateReqVO) {
        // 校验存在
        validateLogisticsExists(updateReqVO.getId());
        // 更新
        LogisticsDO updateObj = LogisticsConvert.INSTANCE.convert(updateReqVO);
        logisticsMapper.updateById(updateObj);
    }

    @Override
    public void deleteLogistics(Integer id) {
        // 校验存在
        validateLogisticsExists(id);
        // 删除
        logisticsMapper.deleteById(id);
    }

    private void validateLogisticsExists(Integer id) {
        if (logisticsMapper.selectById(id) == null) {
            throw exception(500);
        }
    }

    @Override
    public LogisticsDO getLogistics(Integer id) {
        return logisticsMapper.selectById(id);
    }

    @Override
    public List<LogisticsDO> getLogisticsList(Collection<Integer> ids) {
        return logisticsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LogisticsDO> getLogisticsPage(LogisticsPageReqVO pageReqVO) {
        return logisticsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LogisticsDO> getLogisticsList(LogisticsExportReqVO exportReqVO) {
        return logisticsMapper.selectList(exportReqVO);
    }

}
